import requests

from bs4 import BeautifulSoup

import time

def get_soup(url):
    headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    response=requests.get(url,headers=headers)
    soup=BeautifulSoup(response.text)
    return soup

def get_sex(class_name):
    if class_name==['member_boy_ico']:
        return '男'
    else:
        return '女'


def get_info(info_url):
    info_soup=get_soup(info_url)
    titles=info_soup.select('div.pho_info>h4')

    addresses=info_soup.select('span.pr5')
    prices=info_soup.select('#pricePart > div.day_l > span')
    imgs=info_soup.select('div.pho_show_big > div > img#curBigImage')
    names=info_soup.select('a.lorder_name')
    sexs=info_soup.select('div.js_box>div>h6>span')

    for title,address,price,img,name,sex in zip(titles,addresses,prices,imgs,names,sexs):
        data={'title':title.get_text().strip(),
             'address':address.get_text().strip(),
             'price':price.get_text().strip(),
             'img':img.get("src"),
             'sex':get_sex(sex.get("class")),
             }
    print(data)

def get_link(url):
    soup =get_soup(url)
    links=soup.select('#page_list > ul > li >a')
    for link in links:
        href=link.get("href")
        #print(href)
        get_info(href)

if __name__ == '__main__':
    urls=['http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(number) for number in range(1,15)]
    for single_url in urls:
        get_link(single_url)
        time.sleep(15)